import axios from "axios";

//封装api接口
const api = axios.create({
    baseURL: "http://localhost:8088/netshop",
    timeout: 3000,
    headers: {
        "Content-Type": "application/json"
    },
});

// 请求拦截器：根据 config.noAuth 决定是否添加 Authorization 头
api.interceptors.request.use(
    config => {
        const token = localStorage.getItem('token'); // 从 localStorage 读取 token
        // 如果有 token 且 本次请求没有显式设置 noAuth，就自动添加头
        if (token && !config.noAuth) {
            config.headers['Authorization'] = `Bearer ${token}`;
        }
        return config;
    },
    error => Promise.reject(error)
);

//导出封装的api
export default api;